package cn.wubo.beetsql.service.impl;

import cn.wubo.beetsql.dao.DemoTableDao;
import cn.wubo.beetsql.entity.DemoManytable;
import cn.wubo.beetsql.entity.DemoOnetoone;
import cn.wubo.beetsql.entity.DemoTable;
import cn.wubo.beetsql.service.IDemoTableService;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.db.KeyHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DemoTableServiceImpl implements IDemoTableService {
    @Autowired
    private SQLManager sqlManager;

    @Override
    public int add(DemoTable demoTable) {
        return sqlManager.insert(demoTable);
    }

    @Override
    public int update(DemoTable demoTable) {
        return sqlManager.updateById(demoTable);
    }

    @Override
    public int delete(int id) {
        return sqlManager.deleteById(DemoTable.class,id);
    }

    @Override
    public DemoTable getById(int id) {
        return sqlManager.single(DemoTable.class,id);
    }

    @Override
    public List<DemoTable> getAllList() {
        return sqlManager.all(DemoTable.class);
    }

    @Override
    public DemoTable getBatman() {
        List<DemoTable> list = sqlManager.query(DemoTable.class).andEq("name","Batman").select();
        return list.get(0);
    }

    @Override
    public DemoTable getBatmanWithLambda() {
        List<DemoTable> list = sqlManager.query(DemoTable.class).lambda().andEq(DemoTable::getName,"Batman").select();
        return list.get(0);
    }

    @Autowired
    DemoTableDao demoTableDao;

    @Override
    public List<DemoTable> getDemoTableWithOther(DemoTable demoTable) {
        return demoTableDao.selectDemTableAndOther(demoTable);
    }
}
